/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.acraservlet;




import com.mycompany.db.CrashBean;
import com.mycompany.db.DataBase;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.util.Enumeration;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.codehaus.jackson.map.ObjectMapper;

/**
 *
 * @author zhaoyawei
 */
public class CrashServlet extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
            /* TODO output your page here. You may use following sample code. */
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet CrashServlet</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet CrashServlet at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
       System.out.println("params :::::"+request.getParameterMap());
        
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //processRequest(request, response);
//        System.out.println("params :::::"+request.getParameterMap());
       
       CrashBean crash = new CrashBean(request.getParameter("USER_CRASH_DATE"),
                request.getParameter("PHONE_MODEL"),
                request.getParameter("ANDROID_VERSION"),
                request.getParameter("PACKAGE_NAME"),
                request.getParameter("APP_VERSION_NAME"),
                request.getParameter("STACK_TRACE"),
                request.getParameter("LOGCAT"));
       
       System.out.println("logcat is "+request.getParameter("LOGCAT"));
       
       ObjectMapper mapper = new ObjectMapper();
       Writer strWriter = new StringWriter();
       mapper.writeValue(strWriter, crash);
       String jsonString= strWriter.toString();
       System.out.println("json is "+jsonString);
       
       
       System.out.println("1");
        DataBase db = new DataBase();
       System.out.println("2"+db);
        db.setValue(request.getParameter("USER_CRASH_DATE"),jsonString);
      System.out.println("3");
        db.close();
    }
   
    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "deal with the android app crash ,then post some data and so on ";
    }// </editor-fold>
    
    
}
